package com.atguigu.mvc.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HappyHandler {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping(value = "/happy", method = RequestMethod.GET)
    public String happyGet() {

        logger.debug("GET 请求");

        return "target";
    }

    @RequestMapping(value = "/happy", method = RequestMethod.POST)
    public String happyPost() {

        logger.debug("POST 请求");

        return "target";
    }

    @RequestMapping(value = "/happy", method = RequestMethod.PUT)
    public String happyPut() {

        logger.debug("PUT 请求");

        return "target";
    }

    @RequestMapping(value = "/happy", method = RequestMethod.DELETE)
    public String happyDelete() {

        logger.debug("DELETE 请求");

        return "target";
    }

    @RequestMapping(value = "/happy/remove/*", method = RequestMethod.DELETE)
    public String happyRemove() {

        logger.debug("删除操作，DELETE 请求");

        return "target";
    }

    @RequestMapping("/save/message")
    public String saveMessage(@RequestParam("message") String message) {

        logger.debug("message = " + message);

        return "target";
    }

    // /get/happy/1
    // /get/happy/2
    // /get/happy/3
    // 根据 URL 地址的格式，在 @RequestMapping 注解中编写 URL 地址时把动态部分用 {} 标记出来
    // 在 {} 中给动态部分声明一个变量名
    @RequestMapping("/get/happy/{happyId}")
    public String getHappy(

            // 在 handler 方法形参位置使用 @PathVariable 注解引用 URL 地址中 {} 声明的变量名
            // SpringMVC 会负责根据 {} 变量名把 URL 地址中动态部分的具体值给匹配出来，
            // 从这个形参位置传入
            @PathVariable("happyId") Integer happyId
    ) {

        logger.debug("happyId = " + happyId);

        return "target";
    }

    // /save/happy/name/tom/age/20/subject/java
    @RequestMapping("/save/happy/name/{happyName}/age/{happyAge}/subject/{happySubject}")
    public String saveHappy(

            @PathVariable("happyName") String happyName,
            @PathVariable("happyAge") Integer happyAge,
            @PathVariable("happySubject") String happySubject
        ) {

        logger.debug("happyName = " + happyName);
        logger.debug("happyAge = " + happyAge);
        logger.debug("happySubject = " + happySubject);

        return "target";
    }

}
